package com.fanfanwang.ffw.app;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;

import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.common.FileUtils;
import com.fanfanwang.ffw.app.common.StringUtils;
import com.fanfanwang.ffw.app.tenant.R;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.ui.HomeActivity;
import com.fanfanwang.ffw.app.ui.login.GuideAct;
import com.fanfanwang.ffw.app.ui.login.LoginAct;

/**
 * 应用程序启动类：显示欢迎界面并跳转到主界面
 * @version 1.0
 * @created 2012-3-21
 */
public class AppStart extends BaseActivity{

	private static final String TAG  = "AppStart";
	final AppContext ac = (AppContext) getApplication();
	private boolean flag = true;//判断是不是第一次运行程序
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final View view = View.inflate(this, R.layout.start, null);
		LinearLayout wellcome = (LinearLayout) view.findViewById(R.id.app_start_view);
		check(wellcome);
		setContentView(view);

		//渐变展示启动屏
		AlphaAnimation aa = new AlphaAnimation(0.3f,1.0f);
		aa.setDuration(2000);
		view.startAnimation(aa);
		aa.setAnimationListener(new AnimationListener()
		{
			@Override
			public void onAnimationEnd(Animation arg0) {
				//跳转到首页！
				//			redirectTo(ZhiHangAct.class);
				
				//				redirectTo(LoginAct.class);
				//				redirectTo(RegisterAct.class);
				SharedPreferences sharedPreferences = getSharedPreferences(
						"welcome", MODE_WORLD_READABLE);
				flag = sharedPreferences.getBoolean("isFirstUse", true);
				if (flag) {
					redirectTo(GuideAct.class);
					SharedPreferences.Editor editor = sharedPreferences.edit();
					editor.putBoolean("isFirstUse", false);
					editor.commit();
				} else {
					AppConstant.user_id = SharedPreferencesUtil.getInstance(
							getApplicationContext()).getParkingId();
					if(TextUtils.isEmpty(AppConstant.user_id))
					{
					redirectTo(LoginAct.class);
					}else{
						redirectTo(HomeActivity.class);
					}
				}
			}
			@Override
			public void onAnimationRepeat(Animation animation) {}
			@Override
			public void onAnimationStart(Animation animation) {}

		});

	}

	/**
	 * 检查是否需要换图片
	 * @param view
	 */
	private void check(LinearLayout view) {
		String path = FileUtils.getAppCache(this, "welcomeback");
		List<File> files = FileUtils.listPathFiles(path);
		if (!files.isEmpty()) {
			File f = files.get(0);
			long time[] = getTime(f.getName());
			long today = StringUtils.getToday();
			if (today >= time[0] && today <= time[1]) {
				view.setBackgroundDrawable(Drawable.createFromPath(f.getAbsolutePath()));
			}
		}
	}

	/**
	 * 分析显示的时间
	 * @param time
	 * @return
	 */
	private long[] getTime(String time) {
		long res[] = new long[2];
		try {
			time = time.substring(0, time.indexOf("."));
			String t[] = time.split("-");
			res[0] = Long.parseLong(t[0]);
			if (t.length >= 2) {
				res[1] = Long.parseLong(t[1]);
			} else {
				res[1] = Long.parseLong(t[0]);
			}
		} catch (Exception e) {
		}
		return res;
	}

	/**
	 * 跳转到...
	 */
	private void redirectTo(Class toclass){        
		Intent intent = new Intent(this, toclass);
		startActivity(intent);
		finish();
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}
}